//
//  HomeTableViewController.swift
//  iOS07-Weibo-LPK
//
//  Created by 罗培克 on 6/23/15.
//  Copyright © 2015 lpk. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        visitorView?.setUpViewWithInfo("visitordiscover_feed_image_smallicon", message: "关注一些人，回这里看看有什么惊喜", isHome: true)
        
        setupNavigationBar()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        // 开始动画
        visitorView?.startAnim()
    }

    // 自定义导航栏
    private func setupNavigationBar() {
        if sharedUserAccount == nil {
            return
        }
        
        let leftBtn = UIButton()
        leftBtn.frame = CGRectMake(0, 0, 30, 30)
        leftBtn.setImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
        leftBtn.setImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        
        let rightBtn = UIButton()
        rightBtn.frame = CGRectMake(0, 0, 30, 30)
        rightBtn.setImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        rightBtn.setImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        
        // 设置标题
        let name = sharedUserAccount!.name!
        let titleButton = HomeTitleButton()
        titleButton.frame = CGRectMake(0, 0, 120, 30)
        titleButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        titleButton.titleLabel?.font = UIFont.systemFontOfSize(17.0)
        titleButton.setTitle(name, forState: UIControlState.Normal)
        titleButton.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
        titleButton.addTarget(self, action: "titleButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        navigationItem.titleView = titleButton
    }
    
    // 动画转场代理
    let popoverAnimator = PopoverAnimator()
    
    func titleButtonClick() {
        let storyboard = UIStoryboard(name: "FriendGroup", bundle: nil)
        let viewController = storyboard.instantiateViewControllerWithIdentifier("FriendGroup") as! UIViewController
        
        // 设置转场代理
        viewController.transitioningDelegate = popoverAnimator
        let x = (view.bounds.width - 200) * 0.5
        popoverAnimator.presentFrame = CGRectMake(x, 56, 200, 300)
        // 设置转场模式
        viewController.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        presentViewController(viewController, animated: true, completion: nil)
    }
}

